package chapt05.shadow;
// Jet.java
// A hand modeled Jet airplane
// OpenGL SuperBible
// Beginning of OpenGL lighting sample
// Program by Richard S. Wright Jr.

import javax.media.opengl.GL2;
import javax.media.opengl.glu.GLU;

import com.jogamp.opengl.util.gl2.GLUT;

import shared.*;

public class Shadow
{
	static GLU glu = new GLU();
	static GLUT glut = new GLUT();
	
	// Rotation amounts
	static float xRot = 0.0f;
	static float yRot = 0.0f;
	
	// These values need to be available globally
	// Light values and coordinates
	static float ambientLight[] = {0.3f, 0.3f, 0.3f, 1.0f};
	static float diffuseLight[] = {0.7f, 0.7f, 0.7f, 1.0f};
	static float specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
	static float lightPos[] = {-75.0f, 150.0f, -50.0f, 0.0f};
	static float specref[] = {1.0f, 1.0f, 1.0f, 1.0f};
	
	// Transformation matrix to project shadow
	static float shadowMat[] = new float[16];
	
	static void drawJet(GL2 gl2, boolean nShadow)
	{
		float vNormal[] = new float[3]; // Storage for calculated surface normal
		
		// Nose Cone /////////////////////////////
		// Set material color, note we only have to set to black
		// for the shadow once
		if(! nShadow)
			gl2.glColor3ub((byte)128, (byte)128, (byte)128);
		else
			gl2.glColor3ub((byte)0, (byte)0, (byte)0);
		
		// Nose Cone - Points straight down
		// Set material color
		gl2.glBegin(GL2.GL_TRIANGLES);
			gl2.glNormal3f(0.0f, -1.0f, 0.0f);
			gl2.glNormal3f(0.0f, -1.0f, 0.0f);
			gl2.glVertex3f(0.0f, 0.0f, 60.0f);
			gl2.glVertex3f(-15.0f, 0.0f, 30.0f);
			gl2.glVertex3f(15.0f, 0.0f, 30.0f);
			
			// Vertices for this panel
			{
				float vPoints[][] = {
					{15.0f, 0.0f, 30.0f},
					{0.0f, 15.0f, 30.0f},
					{0.0f, 0.0f, 60.0f}
				};
				
				// Calculate the normal for the plane
				M3D.findNormal(vNormal, vPoints[0], vPoints[1], vPoints[2]);
				gl2.glNormal3fv(vNormal, 0);
				gl2.glVertex3fv(vPoints[0], 0);
				gl2.glVertex3fv(vPoints[1], 0);
				gl2.glVertex3fv(vPoints[2], 0);
			}
			
			{
				float vPoints[][] = {
					{0.0f, 0.0f, 60.0f},
					{0.0f, 15.0f, 30.0f},
					{-15.0f, 0.0f, 30.0f}
				};
				
				M3D.findNormal(vNormal, vPoints[0], vPoints[1], vPoints[2]);
				gl2.glNormal3fv(vNormal, 0);
				gl2.glVertex3fv(vPoints[0], 0);
				gl2.glVertex3fv(vPoints[1], 0);
				gl2.glVertex3fv(vPoints[2], 0);
			}
			
			// Body of the Plane ////////////////////////
			{
				float vPoints[][] = {
					{-15.0f, 0.0f, 30.0f},
					{0.0f, 15.0f, 30.0f},
					{0.0f, 0.0f, -56.0f}
				};
				
				M3D.findNormal(vNormal, vPoints[0], vPoints[1], vPoints[2]);
				gl2.glNormal3fv(vNormal, 0);
				gl2.glVertex3fv(vPoints[0], 0);
				gl2.glVertex3fv(vPoints[1], 0);
				gl2.glVertex3fv(vPoints[2], 0);
			}
			
			{
				float vPoints[][] = {
					{0.0f, 0.0f, -56.0f},
					{0.0f, 15.0f, 30.0f},
					{15.0f, 0.0f, 30.0f}
				};
				
				M3D.findNormal(vNormal, vPoints[0], vPoints[1], vPoints[2]);
				gl2.glNormal3fv(vNormal, 0);
				gl2.glVertex3fv(vPoints[0], 0);
				gl2.glVertex3fv(vPoints[1], 0);
				gl2.glVertex3fv(vPoints[2], 0);
			}
			
			gl2.glNormal3f(0.0f, -1.0f, 0.0f);
			gl2.glVertex3f(15.0f, 0.0f, 30.0f);
			gl2.glVertex3f(-15.0f, 0.0f, 30.0f);
			gl2.glVertex3f(0.0f, 0.0f, -56.0f);
			
			///////////////////////////////////////////////
			// Left wing
			// Large triangle for bottom of wing
			{
				float vPoints[][] = {
					{0.0f, 2.0f, 27.0f},
					{-60.0f, 2.0f, -8.0f},
					{60.0f, 2.0f, -8.0f}
				};
				
				M3D.findNormal(vNormal, vPoints[0], vPoints[1], vPoints[2]);
				gl2.glNormal3fv(vNormal, 0);
				gl2.glVertex3fv(vPoints[0], 0);
				gl2.glVertex3fv(vPoints[1], 0);
				gl2.glVertex3fv(vPoints[2], 0);
			}
			
			{
				float vPoints[][] = {
					{60.0f, 2.0f, -8.0f},
					{0.0f, 7.0f, -8.0f},
					{0.0f, 2.0f, 27.0f}
				};
				
				M3D.findNormal(vNormal, vPoints[0], vPoints[1], vPoints[2]);
				gl2.glNormal3fv(vNormal, 0);
				gl2.glVertex3fv(vPoints[0], 0);
				gl2.glVertex3fv(vPoints[1], 0);
				gl2.glVertex3fv(vPoints[2], 0);
			}
			
			{
				float vPoints[][] = {
					{60.0f, 2.0f, -8.0f},
					{-60.0f, 2.0f, -8.0f},
					{0.0f, 7.0f, -8.0f}
				};
				
				M3D.findNormal(vNormal, vPoints[0], vPoints[1], vPoints[2]);
				gl2.glNormal3fv(vNormal, 0);
				gl2.glVertex3fv(vPoints[0], 0);
				gl2.glVertex3fv(vPoints[1], 0);
				gl2.glVertex3fv(vPoints[2], 0);
			}
			
			{
				float vPoints[][] = {
					{0.0f, 2.0f, 27.0f},
					{0.0f, 7.0f, -8.0f},
					{-60.0f, 2.0f, -8.0f}
				};
				
				M3D.findNormal(vNormal, vPoints[0], vPoints[1], vPoints[2]);
				gl2.glNormal3fv(vNormal, 0);
				gl2.glVertex3fv(vPoints[0], 0);
				gl2.glVertex3fv(vPoints[1], 0);
				gl2.glVertex3fv(vPoints[2], 0);
			}
			
			// Tail section///////////////////////////////
	        // Bottom of back fin
			gl2.glNormal3f(0.0f, -1.0f, 0.0f);
			gl2.glVertex3f(-30.0f, -0.50f, -57.0f);
			gl2.glVertex3f(30.0f, -0.50f, -57.0f);
			gl2.glVertex3f(0.0f, -0.50f, -40.0f);
			
			{
				float vPoints[][] = {
					{0.0f, -0.5f, -40.0f},
					{30.0f, -0.5f, -57.0f},
					{0.0f, 4.0f, -57.0f}
				};
				
				M3D.findNormal(vNormal, vPoints[0], vPoints[1], vPoints[2]);
				gl2.glNormal3fv(vNormal, 0);
				gl2.glVertex3fv(vPoints[0], 0);
				gl2.glVertex3fv(vPoints[1], 0);
				gl2.glVertex3fv(vPoints[2], 0);
			}
			
			{
				float vPoints[][] = {
					{0.0f, 4.0f, -57.0f},
					{-30.0f, -0.5f, -57.0f},
					{0.0f, -0.5f, -40.0f}
				};
				
				M3D.findNormal(vNormal, vPoints[0], vPoints[1], vPoints[2]);
				gl2.glNormal3fv(vNormal, 0);
				gl2.glVertex3fv(vPoints[0], 0);
				gl2.glVertex3fv(vPoints[1], 0);
				gl2.glVertex3fv(vPoints[2], 0);
			}
			
			{
				float vPoints[][] = {
					{30.0f, -0.5f, -57.0f},
					{-30.0f, -0.5f, -57.0f},
					{0.0f, 4.0f, -57.0f}
				};
				
				M3D.findNormal(vNormal, vPoints[0], vPoints[1], vPoints[2]);
				gl2.glNormal3fv(vNormal, 0);
				gl2.glVertex3fv(vPoints[0], 0);
				gl2.glVertex3fv(vPoints[1], 0);
				gl2.glVertex3fv(vPoints[2], 0);
			}
			
			{
				float vPoints[][] = {
					{0.0f, 0.5f, -40.0f},
					{3.0f, 0.5f, -57.0f},
					{0.0f, 25.0f, -65.0f}
				};
				
				M3D.findNormal(vNormal, vPoints[0], vPoints[1], vPoints[2]);
				gl2.glNormal3fv(vNormal, 0);
				gl2.glVertex3fv(vPoints[0], 0);
				gl2.glVertex3fv(vPoints[1], 0);
				gl2.glVertex3fv(vPoints[2], 0);
			}
			
			{
				float vPoints[][] = {
					{0.0f, 25.0f, -65.0f},
					{-3.0f, 0.5f, -57.0f},
					{0.0f, 0.5f, -40.0f}
				};
				
				M3D.findNormal(vNormal, vPoints[0], vPoints[1], vPoints[2]);
				gl2.glNormal3fv(vNormal, 0);
				gl2.glVertex3fv(vPoints[0], 0);
				gl2.glVertex3fv(vPoints[1], 0);
				gl2.glVertex3fv(vPoints[2], 0);
			}
			
			{
				float vPoints[][] = {
					{3.0f, 0.5f, -57.0f},
					{-3.0f, 0.5f, -57.0f},
					{0.0f, 25.0f, -65.0f}
				};
				
				M3D.findNormal(vNormal, vPoints[0], vPoints[1], vPoints[2]);
				gl2.glNormal3fv(vNormal, 0);
				gl2.glVertex3fv(vPoints[0], 0);
				gl2.glVertex3fv(vPoints[1], 0);
				gl2.glVertex3fv(vPoints[2], 0);
			}
			
			gl2.glEnd();		
	}
	
	// This function does any needed initialization on the rendering
	// context.
	protected static void setup(GL2 gl2, int width, int height)
	{
		// Any three points on the ground (counter clockwise order)
		float points[][] = {
			{-30.0f, -149.0f, -20.0f},
			{-30.0f, -149.0f, 20.0f},
			{40.0f, -149.0f, 20.0f}
		};
		
		gl2.glEnable(GL2.GL_DEPTH_TEST); // Hidden surface removal
		gl2.glFrontFace(GL2.GL_CCW);     // Counter clock-wise polygons face out
		gl2.glEnable(GL2.GL_CULL_FACE);  // Do not calculate inside of jet
		
		// Setup and enable light 0
		gl2.glLightfv(GL2.GL_LIGHT0, GL2.GL_AMBIENT, ambientLight, 0);
		gl2.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, diffuseLight, 0);
		gl2.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, specular, 0);
		gl2.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, lightPos, 0);
		gl2.glEnable(GL2.GL_LIGHT0);
		
		// Enable color tracking
		gl2.glEnable(GL2.GL_COLOR_MATERIAL);
		
		// Set Material properties to follow glColor values
		gl2.glColorMaterial(GL2.GL_FRONT, GL2.GL_AMBIENT_AND_DIFFUSE);
		
		// All materials hereafter have full specular reflectivity
	    // with a high shine
		gl2.glMaterialfv(GL2.GL_FRONT, GL2.GL_SPECULAR, specref, 0);
		gl2.glMateriali(GL2.GL_FRONT, GL2.GL_SHININESS, 128);
		
		// Light blue background
		gl2.glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
		
		// Get the plane equation from three points on the ground
		float vPlaneEquation[] = new float[4];
		M3D.getPlaneEquation(vPlaneEquation, points[0], points[1], points[2]);
		
		// Calculate projection matrix to draw shadow on the ground
		M3D.makePlanarShadowMatrix(shadowMat, vPlaneEquation, lightPos);
		
		gl2.glEnable(GL2.GL_NORMALIZE);
	}
	
	// Called to draw scene
	protected static void render(GL2 gl2, int w, int h)
	{
		float fAspect;
		
		// Prevent a divide by zero
		if(h == 0)
			h = 1;
		
		// Set Viewport to window dimensions
		gl2.glViewport(0, 0, w, h);
		
		// Reset coordinate system
		gl2.glMatrixMode(GL2.GL_PROJECTION);
		gl2.glLoadIdentity();
		
		fAspect = (float)w / (float)h;
		glu.gluPerspective(60.0f, fAspect, 200.0f, 500.0f);
		
		gl2.glMatrixMode(GL2.GL_MODELVIEW);
		gl2.glLoadIdentity();
		
		// Move out Z axis so we can see everything
		gl2.glTranslatef(0.0f, 0.0f, -400.0f);
		gl2.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, lightPos, 0);
		
		// Clear the window with current clearing color
		gl2.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
		
		// Draw the ground, we do manual shading to a darker green
	    // in the background to give the illusion of depth
		gl2.glBegin(GL2.GL_QUADS);
			gl2.glColor3ub((byte)0, (byte)32, (byte)0);
			gl2.glVertex3f(400.0f, -150.0f, -200.0f);
			gl2.glVertex3f(-400.0f, -150.0f, -200.0f);
			gl2.glColor3ub((byte)0, (byte)255, (byte)0);
			gl2.glVertex3f(-400.0f, -150.0f, 200.0f);
			gl2.glVertex3f(400.0f, -150.0f, 200.0f);
		gl2.glEnd();
		
		// Save the matrix state and do the rotations
		gl2.glPushMatrix();
		
		// Draw jet at new orientation, put light in correct position
	    // before rotating the jet
		gl2.glEnable(GL2.GL_LIGHTING);
		gl2.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, lightPos, 0);
		gl2.glRotatef(xRot, 1.0f, 0.0f, 0.0f);
		gl2.glRotatef(yRot, 0.0f, 1.0f, 0.0f);
		
		drawJet(gl2, false);
		
		// Restore original matrix state
		gl2.glPopMatrix();
		
		// Get ready to draw the shadow and the ground
		// First disable lighting and save the projection state
		gl2.glDisable(GL2.GL_DEPTH_TEST);
		gl2.glDisable(GL2.GL_LIGHTING);
		gl2.glPushMatrix();
		
		// Multiply by shadow projection matrix
		gl2.glMultMatrixf(shadowMat, 0);
		
		// Now rotate the jet around in the new flattened space
		gl2.glRotatef(xRot, 1.0f, 0.0f, 0.0f);
		gl2.glRotatef(yRot, 0.0f, 1.0f, 0.0f);
		
		// Pass true to indicate drawing shadow
		drawJet(gl2, true);
		
		// Restore the projection to normal
		gl2.glPopMatrix();
		
		// Draw the light source
		gl2.glPushMatrix();
		gl2.glTranslatef(lightPos[0], lightPos[1], lightPos[2]);
		gl2.glColor3ub((byte)255, (byte)255, (byte)0);
		glut.glutSolidSphere(5.0f, 10, 10);
		gl2.glPopMatrix();
		
		// Restore lighting state variables
		gl2.glEnable(GL2.GL_DEPTH_TEST);
		
		// Display the results
		gl2.glFlush();
	}
}